const {
	checkVersionMethod,
	loadVersionMethod,
	backVersionMethod,
	onDblClickRow,
	onSelectPage,
} = require('./api-version-methods')

module.exports = function(apiComp, projectId) {
	checkVersionBtnInit(apiComp)
	versionDialogInit(apiComp)
	versionTableInit(apiComp, projectId)
}

/**
 * 初始化版本查看按钮
 * @param  {[type]} apiComp [description]
 * @return {[type]}         [description]
 */
function checkVersionBtnInit(apiComp) {
	let {
		btnVersion,
		btnVersionBack,
	} = apiComp;
	// 版本查看按钮
	btnVersion.linkbutton({
		width: "80px",
		iconCls: 'icon-reload',
		text: '查看版本',
		// disabled: true,
		onClick() {
			checkVersionMethod.call(apiComp)
		},
	});

	btnVersionBack.linkbutton({
		width: "120px",
		iconCls: 'icon-back',
		text: '返回最新版本',
		disabled: true,
		onClick() {
			backVersionMethod.call(apiComp)
		},
	});
}

/**
 * 初始化查看版本对话框
 * @param  {[type]} apiComp [description]
 * @return {[type]}         [description]
 */
function versionDialogInit(apiComp) {
	let {
		versionDialog
	} = apiComp
	versionDialog.dialog({
		title: '版本管理',
		top: 100,
		left: 300,
		width: 800,
		height: 460,
		closed: true,
		modal: true,
		buttons: [
			// {
			// 	text: '切换',
			// 	iconCls: 'icon-edit',
			// 	handler: function() {
			// 		loadVersionMethod.call(apiComp)
			// 	}
			// }, 
			{
				text: '查看',
				iconCls: 'icon-redo',
				handler: function() {
					loadVersionMethod.call(apiComp)
				}
			}, {
				text: '取消',
				iconCls: 'icon-cancel',
				handler: function() {
					versionDialog.dialog('close')
				}
			}
		]
	})
}

/**
 * 初始化接口版本列表
 * @param  {[type]} apiComp [description]
 * @return {[type]}         [description]
 */
function versionTableInit(apiComp, projectId) {
	let {
		versionTable,
	} = apiComp
	versionTable.datagrid({
		fit: true,
		rownumbers: true,
		fitColumns: true,
		singleSelect: true,
		// url: '/ult/project/version',
		// queryParams: {
		// 	project_id: projectId,
		// },
		pagination: true,
		columns: [
			[{
				field: 'version',
				title: '版本号',
				width: 20
			}, {
				field: 'edit_user',
				title: '操作人',
				width: 30,
			}, {
				field: 'modify_date',
				title: '修改时间',
				width: 35,
			}, {
				field: 'description',
				title: '提交说明',
				width: 80,
				formatter(value, row, index) {
					try {
						value = JSON.parse(value)
					} catch (e) {
						console.log(value, e)
					}
					return value.info || value || '没有说明!'
				},
			}]
		],
		onDblClickRow(index, row) {
			onDblClickRow.call(apiComp, index, row)
		},
	});
	// 分页控制
	versionTable.datagrid('getPager').pagination({
		/**
		 * 选择页码
		 * @param  {[type]} pageNumber [description]
		 * @param  {[type]} pageSize   [description]
		 * @return {[type]}            [description]
		 */
		onSelectPage(pageNumber, pageSize) {
			onSelectPage.call(apiComp, pageNumber, pageSize)
		},
	})
}